package com.yuexun.dhdoctor.module.survey.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.module.survey.bean.SurveyAnswerDtlBean;
import com.yuexun.dhdoctor.module.survey.bean.SurveyDtlBean;

import java.util.List;

/**
 * Created by wangt on 2017/8/23.
 */

public class SurveyTopicAdapter extends BaseAdapter {

    private Context context;
    private List<SurveyDtlBean.DtlListBean> topicList;
    private LayoutInflater mInflater;
    //可点击
    private boolean isSelect;
    //答案
    private List<SurveyAnswerDtlBean.ListBeanX> answerList;

    public SurveyTopicAdapter(Context context, List<SurveyDtlBean.DtlListBean> topicList, boolean isSelect, List<SurveyAnswerDtlBean.ListBeanX> answerList) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
        this.topicList = topicList;
        this.isSelect = isSelect;
        this.answerList = answerList;
    }

    @Override
    public int getCount() {
        return topicList == null ? 0 : (topicList.isEmpty() ? 0 : topicList.size());
    }

    @Override
    public Object getItem(int position) {
        return topicList == null ? null : topicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_survey_topic, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.topic_name);
            viewHolder.checkGroup = (LinearLayout) convertView.findViewById(R.id.checkbox_group);
            viewHolder.etOther = (EditText) convertView.findViewById(R.id.et_other);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String showType = "";
        if (topicList.get(position).getShowType().equals("1") || topicList.get(position).getShowType().equals("2")) {  //1单选 2单选其他
            if (topicList.get(position).getShowType().equals("1")) {
                showType = "单选项";
            } else {
                showType = "单选项+其他";
            }

            for (int i = 0; i < topicList.get(position).getList().size(); i++) {
                final CheckBox checkBox = new CheckBox(context);
                RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, 100);
                lp.setMargins(5, 0, 0, 0);
                checkBox.setLayoutParams(lp);
                checkBox.setButtonDrawable(R.drawable.radio_blue_selector);
                checkBox.setPadding(40, 0, 0, 0);
                if (topicList.get(position).getShowType().equals("2") && i == (topicList.get(position).getList().size() - 1)) {
                    if (topicList.get(position).getList().get(i).getDtlName().equals("-9999")) {
                        checkBox.setText("其他");
                        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    viewHolder.etOther.requestFocus();
                                    viewHolder.etOther.setFocusable(true);
                                    viewHolder.etOther.setFocusableInTouchMode(true);
                                } else {
                                    viewHolder.etOther.setFocusable(false);
                                    viewHolder.etOther.setFocusableInTouchMode(false);
                                }
                            }
                        });
                        viewHolder.etOther.setVisibility(View.VISIBLE);
                    } else {
                        checkBox.setText(topicList.get(position).getList().get(i).getDtlName());
                        viewHolder.etOther.setVisibility(View.GONE);
                    }
                } else {
                    checkBox.setText(topicList.get(position).getList().get(i).getDtlName());
                    viewHolder.etOther.setVisibility(View.GONE);
                }
                /*-------------显示答案-----------------*/
                if (answerList.size() != 0) {
                    if (answerList.get(position).getList().get(i).getDtlAnswer() == 1) {
                        checkBox.setChecked(true);
                    }
                    if (!isSelect) {  //展示时 有其他-输入框显示 无其他-输入框隐藏
                        if (!TextUtils.isEmpty(answerList.get(position).getList().get(i).getDtlValue())) {
                            viewHolder.etOther.setText(answerList.get(position).getList().get(i).getDtlValue());
                        } else {
                            viewHolder.etOther.setVisibility(View.GONE);
                        }
                    }
                }
                /*-------------是否可点击-----------------*/
                if (!isSelect) {
                    checkBox.setClickable(false);
                    viewHolder.etOther.setFocusable(false);
                    viewHolder.etOther.setFocusableInTouchMode(false);
                } else {
                    checkBox.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final int childcount = viewHolder.checkGroup.getChildCount();
                            for (int i = 0; i < childcount; i++) {
                                CheckBox view = (CheckBox) viewHolder.checkGroup.getChildAt(i);
                                if (view.isChecked()) {
                                    view.setChecked(false);
                                }
                                checkBox.setChecked(true);
                            }
                        }
                    });
                }
                viewHolder.checkGroup.addView(checkBox);
            }
        } else if (topicList.get(position).getShowType().equals("3") || topicList.get(position).getShowType().equals("4")) { //3复选 4复选其他
            if (topicList.get(position).getShowType().equals("3")) {
                showType = "多选项";
            } else {
                showType = "多选项+其他";
            }

            for (int i = 0; i < topicList.get(position).getList().size(); i++) {
                CheckBox checkBox = new CheckBox(context);
                RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, 100);
                lp.setMargins(5, 0, 0, 0);
                checkBox.setLayoutParams(lp);
                checkBox.setButtonDrawable(R.drawable.radio_blue_selector);
                checkBox.setPadding(40, 0, 0, 0);
                if (topicList.get(position).getShowType().equals("4") && i == (topicList.get(position).getList().size() - 1)) {
                    if (topicList.get(position).getList().get(i).getDtlName().equals("-9999")) {
                        checkBox.setText("其他");
                        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    viewHolder.etOther.requestFocus();
                                    viewHolder.etOther.setFocusable(true);
                                    viewHolder.etOther.setFocusableInTouchMode(true);
                                } else {
                                    viewHolder.etOther.setFocusable(false);
                                    viewHolder.etOther.setFocusableInTouchMode(false);
                                }
                            }
                        });
                        viewHolder.etOther.setVisibility(View.VISIBLE);
                    } else {
                        checkBox.setText(topicList.get(position).getList().get(i).getDtlName());
                        viewHolder.etOther.setVisibility(View.GONE);
                    }
                } else {
                    checkBox.setText(topicList.get(position).getList().get(i).getDtlName());
                    viewHolder.etOther.setVisibility(View.GONE);
                }
                /*-------------显示答案-----------------*/
                if (answerList.size() != 0) {
                    if (answerList.get(position).getList().get(i).getDtlAnswer() == 1) {
                        checkBox.setChecked(true);
                    }
                    if (!isSelect) {  //展示时 有其他-输入框显示 无其他-输入框隐藏
                        if (!TextUtils.isEmpty(answerList.get(position).getList().get(i).getDtlValue())) {
                            viewHolder.etOther.setText(answerList.get(position).getList().get(i).getDtlValue());
                        } else {
                            viewHolder.etOther.setVisibility(View.GONE);
                        }
                    }
                }
                /*-------------是否可点击-----------------*/
                if (!isSelect) {
                    checkBox.setClickable(false);
                    viewHolder.etOther.setFocusable(false);
                    viewHolder.etOther.setFocusableInTouchMode(false);
                }
                viewHolder.checkGroup.addView(checkBox);
            }
        } else if (topicList.get(position).getShowType().equals("5")) {                  //文本框
            showType = "文本";
            viewHolder.etOther.setHint("请输入内容");
            viewHolder.etOther.requestFocus();
            viewHolder.etOther.setFocusable(true);
            viewHolder.etOther.setFocusableInTouchMode(true);

            /*-------------显示答案-----------------*/
            if (answerList.size() != 0) {
                if (!TextUtils.isEmpty(answerList.get(position).getDtlValue())) {
                    viewHolder.etOther.setText(answerList.get(position).getDtlValue());
                }
            }

            /*-------------是否可点击-----------*/
            if (!isSelect) {
                viewHolder.etOther.setFocusable(false);
                viewHolder.etOther.setFocusableInTouchMode(false);
            }
        }
        viewHolder.tvName.setText(position + 1 + "、" + topicList.get(position).getDtlName() + "(" + showType + ")");

        return convertView;
    }

    class ViewHolder {
        TextView tvName;
        LinearLayout checkGroup;
        EditText etOther;
    }
}
